﻿package demo.app.factory
{
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Math;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2FixtureDef;
	
	import demo.app.Constants;
	import demo.app.model.vo.PolyShapeVO;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	public class ShapeFactory
	{
		
		/**
		 * Create a body definition for any shape.
		 * @param shapeSprite
		 * @return 
		 * 
		 */
		public static function generateBodyDef(shapeSprite:Sprite, xPos:int, yPos:int):b2BodyDef
		{
			var result:b2BodyDef = new b2BodyDef();
			
			result.position.Set(xPos/Constants.PHYS_SCALE, yPos/ Constants.PHYS_SCALE);
			result.type = b2Body.b2_dynamicBody;
			
			// Set the userData (object Sprite)
			result.userData = shapeSprite;
			
			return result;
		}  
		
		public static function generateSquareDef(polyVO:PolyShapeVO):b2FixtureDef
		{
			var result:b2PolygonShape = new b2PolygonShape();

			var fixtureDef:b2FixtureDef = new b2FixtureDef();
			fixtureDef.shape = result;
			fixtureDef.friction = Constants.FRICTION;
			fixtureDef.restitution = Constants.RESTITUTION;
			fixtureDef.density = 50; // static bodies require zero density

			result.SetAsBox(polyVO.radius / Constants.PHYS_SCALE, polyVO.radius / Constants.PHYS_SCALE);
			
			return fixtureDef;
		}
		
		public static function generateSquareSprite(polyVO:PolyShapeVO):Sprite
		{
			var polyClip:Sprite = new Sprite();
			polyClip.addChild(new Box2DImage());
			var matrix:Matrix = new Matrix(); // creates an identity matrix 
			matrix.scale(1,-1)
			matrix.translate(0,polyClip.height)

     		polyClip.cacheAsBitmapMatrix = matrix; 
     		polyClip.cacheAsBitmap = true;
			
			return polyClip;
		}
		
		public static function radians(n:Number):Number 
		{
			return(Math.PI/180*n);
		}
		
	}
}